package com.second.app.thread.reentrant.conditionerror;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author gyz
 * @date 2021/2/1 14:51
 * 注意在使用condition.await()方法之前，必须调用lock.lock()方法获得同步锁对象。
 * condition.await()方法，类似于object中的wait()方法,让当前线程等待，并释放锁。
 */
public class MyService implements Runnable {
    private Lock lock = new ReentrantLock();

    private Condition condition = lock.newCondition();

    @Override
    public void run() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "获取到锁了"+lock.toString());
            condition.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            System.out.println("锁释放了");
        }
    }
}
